<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/users' }">用户管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/users' }">用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-input
      v-model="val"
      @blur="search"
      placeholder="请输入内容"
      style="width: 400px; height: 25px"
    ></el-input
    ><el-button type="primary" @click="add">添加用户</el-button>
    <!-- 弹出层 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 表格 -->
    <el-table :data="tabledata" style="width: 100%">
      <el-table-column type="index" width="50" label="#"> </el-table-column>

      <el-table-column prop="username" label="姓名"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column prop="role_name" label="角色"></el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="state(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>
          <el-button
            type="warning"
            icon="el-icon-s-tools"
            @click="status1(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 角色模态框 -->
    <el-dialog title="分配角色" :visible.sync="dialogVisible2" width="30%">
      <p>当前的用户:{{ row.username }}</p>
      <p>当前的角色:{{ row.role_name }}</p>

      <el-select v-model="value" placeholder="分配新角色">
        <el-option
          :label="item.roleName"
          :value="item.id"
          v-for="(item, index) in list"
          :key="item.id"
        ></el-option>
      </el-select>
      <el-button type="primary" @click="editform1">确定</el-button>
      <el-button @click="resetForm('Form')">重置</el-button>
    </el-dialog>
    <!-- 编辑模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible1" width="30%">
      <el-form
        :model="Form"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="Form.username" disabled></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="Form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="Form.mobile"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="editform">立即创建</el-button>
          <el-button @click="resetForm('Form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pages.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pages.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  menu,
  users,
  addUser,
  deleteUser,
  editok,
  status,
  gteroled,
} from "../utils/api";
export default {
  data() {
    return {
      list: [],
      row: {},
      value: "",
      tabledata: [
        { username: "", email: "", mobile: "", role_name: "", mg_state: "" },
      ],
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
      pages: {
        pagenum: 1,
        pagesize: 10,
      },
      val: "",
      total: 0,
      dialogVisible: false, //添加
      dialogVisible1: false, //编辑
      dialogVisible2: false, //角色
      ruleForm: {},
      Form: {}, //编辑数据
      Form1: {}, //角色数据
    };
  },
  methods: {
    // 添加角色
    editform1() {
      let params = { rid: this.from1.region, id: this.roId };
      this.dialogVisible2 = true;
      menu(params).then((res) => {
        console.log(res);
      });
    },

    // 状态切换
    state(row) {
      status(row.id, row.mg_state).then((res) => {
        console.log(res);
        this.getData();
        if (res.data.meta.status == 200) {
          this.$message({
            showClose: true,
            message: "状态修改成功",
            type: "success",
          });
        }
      });
    },
    status1(row) {
      this.dialogVisible2 = true;
      this.row = row;
    },

    // 搜索
    search() {
      this.tabledata = this.tabledata.filter((item) => {
        return item.username.includes(this.val);
      });
    },
    // 编辑
    edit(row) {
      this.dialogVisible1 = true;

      this.Form = JSON.parse(JSON.stringify(row));
      console.log();
    },
    editform() {
      console.log(this.Form);
      editok(this.Form).then((res) => {
        console.log(res.data);
        this.getData();
        this.dialogVisible1 = false;
        if (res.data.meta.status == 200) {
          this.$message({
            showClose: true,
            message: "编辑成功",
            type: "success",
          });
        }
      });
    },

    // 删除用户
    del(row) {
      console.log(row);
      deleteUser(row.id).then((res) => {
        console.log(res);
        this.getData();
        if (res.data.meta.status == 200) {
          this.$message({
            showClose: true,
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 添加用户
    add() {
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addUser(this.ruleForm).then((res) => {
            console.log(res);
            this.ruleForm = res.data.data;
            this.getData();
            if (res.data.meta.status == 201) {
              this.$message({
                showClose: true,
                message: "添加成功",
                type: "success",
              });
            }
            //   关闭模态框
            this.dialogVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.pages.pagesize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pages.pagenum = val;
      this.getData();
    },
    // 数据
    getData() {
      users(this.pages).then((res) => {
        console.log(res);
        this.tabledata = res.data.data.users;
        this.total = res.data.data.total;
        if (res.data.meta.status === 200) {
          this.$message({
            showClose: true,
            message: "获取列表成功",
            type: "success",
          });
        }
      });
      // 角色
      gteroled().then((res) => {
        console.log(res);
        this.list = res.data.data;
      });
    },
  },
  created() {
    this.getData();
  },
  mounted() {},
  components: {},
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped></style>
